﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework
{
    public class SqlDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Email> Emails { get; set; }

        public SqlDbContext() : base("17bang")
        {
            Database.Log = Console.WriteLine;
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Email>()
                .HasRequired(e => e.User)
                .WithRequiredDependent();

            modelBuilder.Entity<User>()
                .ToTable("Register")
                .HasKey(m => m.Id)
                //.HasKey(m => m.Name)
                .Property(m => m.Name).HasMaxLength(256)
                .HasColumnName("UserName");
            
            modelBuilder.Entity<User>()    
                .Property(m => m.Password).IsRequired();
            
            modelBuilder.Entity<User>()
            .Ignore(m => m.FailedTry);

            modelBuilder.Entity<User>()
                //.HasCheckConstraint("CK_CreateTime", "CreateTime > '2000-1-1'")
                .HasIndex(m => m.CreateTime).IsUnique();

            base.OnModelCreating(modelBuilder);
        }
    }
}
